package com.vay.de.dang.online.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.vay.de.dang.online.R;

/**
 * ProjectName: NsVnm
 * Author     : 南山
 * Date       : 2023/12/6
 * Describe   : 类描述
 */
public class ImageCheckBox extends androidx.appcompat.widget.AppCompatImageView {

    private int flag;
    private View.OnClickListener viewClick;

    public ImageCheckBox(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setOnClickListener(v -> {
            flag = (++flag) % 2;
            updateIcon();
            if (viewClick != null) {
                viewClick.onClick(v);
            }
        });
        updateIcon();
    }

    private void updateIcon() {
        setImageResource(flag == 0 ? R.mipmap.ic_cb_nor : R.mipmap.ic_cb_pre);
    }

    public boolean isBoxChecked() {
        return flag == 1;
    }

    public void setViewClick(View.OnClickListener click) {
        viewClick = click;
    }

    public void setBoxChecked(boolean enable) {
        flag = enable ? 1 : 0;
        updateIcon();
    }

}
